package com.example.demo.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.port.fee.dao.FeeTypeMapper;
import com.port.fee.entity.FeeType;
import com.port.fee.service.FeeTypeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 费用类型服务实现
 */
@Service
public class FeeTypeServiceImpl extends ServiceImpl<FeeTypeMapper, FeeType> implements FeeTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FeeTypeServiceImpl.class);

    @Override
    public FeeType getFeeTypeById(Long feeTypeId) {
        return getById(feeTypeId);
    }

    @Override
    public FeeType getFeeTypeByCode(String feeCode) {
        if (StringUtils.isBlank(feeCode)) {
            return null;
        }

        LambdaQueryWrapper<FeeType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeeType::getFeeCode, feeCode);

        return getOne(queryWrapper);
    }

    @Override
    public Page<FeeType> listFeeTypes(FeeType feeType, int pageNum, int pageSize) {
        LambdaQueryWrapper<FeeType> queryWrapper = new LambdaQueryWrapper<>();

        // 根据费用名称模糊查询
        if (StringUtils.isNotBlank(feeType.getFeeName())) {
            queryWrapper.like(FeeType::getFeeName, feeType.getFeeName());
        }

        // 根据费用编码查询
        if (StringUtils.isNotBlank(feeType.getFeeCode())) {
            queryWrapper.eq(FeeType::getFeeCode, feeType.getFeeCode());
        }

        // 根据币种查询
        if (StringUtils.isNotBlank(feeType.getCurrency())) {
            queryWrapper.eq(FeeType::getCurrency, feeType.getCurrency());
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(FeeType::getCreateTime);

        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public boolean addFeeType(FeeType feeType) {
        // 检查费用编码是否已存在
        if (checkFeeCodeExists(feeType.getFeeCode(), null)) {
            LOGGER.error("添加费用类型失败：费用编码 {} 已存在", feeType.getFeeCode());
            return false;
        }

        // 设置默认值
        if (StringUtils.isBlank(feeType.getCurrency())) {
            feeType.setCurrency("CNY");
        }

        feeType.setCreateTime(new Date());
        feeType.setUpdateTime(new Date());

        return save(feeType);
    }

    @Override
    public boolean updateFeeType(FeeType feeType) {
        // 检查费用编码是否已存在
        if (checkFeeCodeExists(feeType.getFeeCode(), feeType.getFeeTypeId())) {
            LOGGER.error("修改费用类型失败：费用编码 {} 已存在", feeType.getFeeCode());
            return false;
        }

        feeType.setUpdateTime(new Date());

        return updateById(feeType);
    }

    @Override
    public boolean deleteFeeType(Long feeTypeId) {
        // TODO: 检查是否有关联的费用记录

        return removeById(feeTypeId);
    }

    @Override
    public boolean checkFeeCodeExists(String feeCode, Long excludeId) {
        if (StringUtils.isBlank(feeCode)) {
            return false;
        }

        LambdaQueryWrapper<FeeType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FeeType::getFeeCode, feeCode);

        // 排除指定ID
        if (excludeId != null) {
            queryWrapper.ne(FeeType::getFeeTypeId, excludeId);
        }

        return count(queryWrapper) > 0;
    }
}